package com.yidu.ladder.base.service.impl;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.yidu.ladder.base.dao.AreaDao;
import com.yidu.ladder.base.domain.Area;
import com.yidu.ladder.base.domain.AreaVo;
import com.yidu.ladder.base.service.AreaService;
import com.yidu.ladder.util.ExcelUtil;
import com.yidu.ladder.util.IDGenerator;
import com.yidu.ladder.util.PinYin4jUtils;
/**
 * 区域业务实现类
 * @author lw
 * @since 2020/1/10
 */
@Service
public class AreaServiceImpl implements AreaService{
	/**
	 * 保留对区域数据访问层的依赖注入
	 */
	@Autowired
	private AreaDao areaDao;
	
	public AreaDao getAreaDao() {
		return areaDao;
	}

	public void setAreaDao(AreaDao areaDao) {
		this.areaDao = areaDao;
	}

	public AreaServiceImpl() {
		
	}

	public AreaServiceImpl(AreaDao areaDao) {
		super();
		this.areaDao = areaDao;
	}

	@Override
	public boolean addArea(Area area) {
		return areaDao.addArea(area);
	}

	@Override
	public void saveBath(List<String[]> list) {
		//循环遍历数组
		for (String[] strings : list) {
			Area  area = new Area();
			//给区域Id赋值
			area.setAreaId(IDGenerator.getId());
			//给省赋值
			area.setProvince(strings[0]);
			//给市赋值
			area.setCity(strings[1]);
			//给县赋值
			area.setDistrict(strings[2]);
			//给邮编赋值
			area.setPostcode(strings[3]);
			//给是否外网赋值
			area.setIsOuterNet("Y");
			//给是否郊区县赋值
			area.setIsSuburban("Y");
			//给区域级别赋值
			area.setAreaLevel(strings[4]);
			//给所属区域赋值
			area.setAttrArea(strings[5]);
			//给城市级别赋值
			area.setCityLevel(strings[6]);
			//给状态赋值
			area.setUseable("Y");
			//调用PinYin4j工具类的方法来获取省的首字母大写
			String[] provinces = PinYin4jUtils.getHeadByString(strings[0]);
			//调用PinYin4j工具类的方法来获取市的首字母大写
			String[] citys = PinYin4jUtils.getHeadByString(strings[1]);
			//调用PinYin4j工具类的方法来获取市的县字母大写
			String[] districts = PinYin4jUtils.getHeadByString(strings[2]);
			
			//定义空的字符串来接收省的简码
			String shortCodeP = "";
			//用for循环来获取省的字母
			for (int i = 0; i < provinces.length-1; i++) {
				//将处理过后的省简码放入到空的字符串中
				shortCodeP = shortCodeP + provinces[i];
			}
			//定义空的字符串来接收市的简码
			String shortCodeC = "";
			//用for循环来获取市的字母
			for (int i = 0; i < citys.length-1; i++) {
				//将处理过后的市简码放入到空的字符串中
				shortCodeC = shortCodeC + citys[i];
			}
			//定义空的字符串来接收县的简码
			String shortCodeD = "";
			//用for循环来获取县的字母
			for (int i = 0; i < districts.length-1; i++) {
				//将处理过后的县简码放入到空的字符串中
				shortCodeD = shortCodeD + districts[i];
			}
			//将得到的省、市、县赋值给简码
			area.setShortcode(shortCodeP+shortCodeC+shortCodeD);
			 //调用PinYin4j工具类的方法来获取市的中文全拼
			String[] cityCodes = PinYin4jUtils.stringToPinyin(strings[1]);
			//定义空的字符串来接收市的城市编码
			String cityCodeC = "";
			//用for循环来获取市的字母全拼
			for (int i = 0; i < citys.length-1; i++) {
				//将处理过后的市城市编码放入到空的字符串中
				cityCodeC = cityCodeC + cityCodes[i];
			}
			//将获取到的市赋值给城市编码
			area.setCitycode(cityCodeC);
			//将区域信息放入到添加方法中
			this.addArea(area);
		}
	}

	@Override
	public Map<String, Object> findAllAreaWithPage(AreaVo areaVo) {
		//计算分页
		areaVo.setPage((areaVo.getPage()-1)*areaVo.getLimit());
		//查询所有
		List<Area> areaList = areaDao.findAllAreaWithPage(areaVo);
		//计数
		int total = areaDao.countArea(areaVo);
		//定义一个Map对象
		Map<String, Object> map = new HashMap<String, Object>();
		//将数据封装到结果映射中 
		map.put("code", 0);
		map.put("msg", "");
		map.put("count", total);
		map.put("data", areaList);
		return map;
	}
	

	@Override
	public boolean updataArea(Area area) {
		int result = areaDao.updataArea(area);
		return result == 0 ? false : true;
	}

	@Override
	public boolean frozenORrecoverArea(String areaIdStr,boolean flag) {
		//定义一个map对象
		Map<String, Object> paramMap = new HashMap<String, Object>();
		//判断进行冻结还是还原
		char useable = flag == true ? 'N' : 'Y';
		//根据逗号分割ID
		String[] areaIds = areaIdStr.split(",");
		//定义执行结果返回的总数 因为成功返回1，所以把所有执行结果加起来正好就等于ID的总数
		int total = 0;
		for (int i =0; i < areaIds.length; i++) {
			//添加ID
			paramMap.put("areaId", areaIds[i]);
			//添加要修改的状态
			paramMap.put("useable", useable);
			//调用逻辑删除或还原方法
			int result = areaDao.frozenORrecoverArea(paramMap);
			//计数
			total += result;
		}
		return total == areaIds.length ? true : false;
	}

	@Override
	public void insertdb(InputStream in, MultipartFile file) throws Exception {
		//调用工具类
		List<Object[]> listob = ExcelUtil.getBankListByExcel(in,file.getOriginalFilename());
		//创建一个区域集合对象
	    List<Area> areaList = new ArrayList<Area>();
	    //遍历listob数据，把数据放到List中
	    for (int i = 0; i < listob.size(); i++) {
	    	//创建一个Object数组
	        Object[] ob = listob.get(i);
	        //创建一个区域对象
	        Area area = new Area();
	        //给区域Id赋值
			area.setAreaId(IDGenerator.getId());
			//给省赋值
			 area.setProvince(String.valueOf(ob[0]));
			//给市赋值
			area.setCity(String.valueOf(ob[1]));
			//给县赋值
			area.setDistrict(String.valueOf(ob[2]));
			//给邮编赋值
			area.setPostcode(String.valueOf(ob[3]));
			//给是否外网赋值
			area.setIsOuterNet("Y");
			//给是否郊区县赋值
			area.setIsSuburban("Y");
			//给区域级别赋值
			area.setAreaLevel(String.valueOf(ob[4]));
			//给所属区域赋值
			area.setAttrArea(String.valueOf(ob[5]));
			//给城市级别赋值
			area.setCityLevel(String.valueOf(ob[6]));
			//给状态赋值
			area.setUseable("Y");
			//调用PinYin4j工具类的方法来获取省的首字母大写
			String[] provinces = PinYin4jUtils.getHeadByString(String.valueOf(ob[0]));
			//调用PinYin4j工具类的方法来获取市的首字母大写
			String[] citys = PinYin4jUtils.getHeadByString(String.valueOf(ob[1]));
			//调用PinYin4j工具类的方法来获取市的县字母大写
			String[] districts = PinYin4jUtils.getHeadByString(String.valueOf(ob[2]));
			
			//定义空的字符串来接收省的简码
			String shortCodeP = "";
			//用for循环来获取省的字母
			for (int j = 0; j < provinces.length-1; j++) {
				//将处理过后的省简码放入到空的字符串中
				shortCodeP = shortCodeP + provinces[j];
			}
			//定义空的字符串来接收市的简码
			String shortCodeC = "";
			//用for循环来获取市的字母
			for (int j = 0; j < citys.length-1; j++) {
				//将处理过后的市简码放入到空的字符串中
				shortCodeC = shortCodeC + citys[j];
			}
			//定义空的字符串来接收县的简码
			String shortCodeD = "";
			//用for循环来获取县的字母
			for (int j = 0; j < districts.length-1; j++) {
				//将处理过后的县简码放入到空的字符串中
				shortCodeD = shortCodeD + districts[j];
			}
			//将得到的省、市、县赋值给简码
			area.setShortcode(shortCodeP+shortCodeC+shortCodeD);
			 //调用PinYin4j工具类的方法来获取市的中文全拼
			String[] cityCodes = PinYin4jUtils.stringToPinyin(String.valueOf(ob[1]));
			//定义空的字符串来接收市的城市编码
			String cityCodeC = "";
			//用for循环来获取市的字母全拼
			for (int j = 0; j < citys.length-1; j++) {
				//将处理过后的市城市编码放入到空的字符串中
				cityCodeC = cityCodeC + cityCodes[j];
			}
			//将获取到的市赋值给城市编码
			area.setCitycode(cityCodeC);
			//将区域对象添加到区域集合中
			areaList.add(area);
	    }
	    //调用添加方法
	    areaDao.insertdb(areaList);
	}

	@Override
	public List<Area> findAll() {
		List<Area> areaList = areaDao.findAll();
		return areaList;
	}

	@Override
	public List<Area> queryAllProvince() {
		List<Area> areaList = areaDao.queryAllProvince();
		return areaList;
	}

	@Override
	public List<Area> queryAllCity(String province) {
		List<Area> areaList = areaDao.queryAllCity(province);
		return areaList;
	}

	@Override
	public List<Area> queryAllDistrict(String city) {
		List<Area> areaList = areaDao.queryAllDistrict(city);
		return areaList;
	}

}
